<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="popUp.$state.FromState"
        @close="popUp.$state.FromState = false"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef1"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef1)"
                    :model="formData"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem label="配件名称" type="string" v-model="formData!.name" prop="name" placeholder="请输入配件名称" />
                    <FormItem
                        label="配件成本"
                        type="number"
                        prop="parts_cost"
                        :input-attr="{ step: 1 }"
                        v-model.number="formData!.parts_cost"
                        placeholder="请输入配件成本"
                    />
                    <FormItem
                        label="配件价格"
                        type="number"
                        prop="parts_price"
                        :input-attr="{ step: 1 }"
                        v-model.number="formData!.parts_price"
                        placeholder="请输入配件价格"
                    />
                    <FormItem
                        label="用途信息"
                        type="remoteSelect"
                        v-model="formData!.purpose_info_id"
                        prop="purpose_info_id"
                        :input-attr="{ pk: 'ba_purpose_info.id', field: 'purpose_name', remoteUrl: '/admin/purpose.Info/index' }"
                        placeholder="请选择用途信息"
                    />
                    <FormItem
                        label="供应商信息"
                        type="remoteSelect"
                        v-model="formData!.supplier_info_id"
                        prop="supplier_info_id"
                        :input-attr="{ pk: 'ba_supplier_info.id', field: 'supplier', remoteUrl: '/admin/supplier.Info/index' }"
                        placeholder="请选择供应商信息"
                    />
                    <FormItem
                        label="备注"
                        type="textarea"
                        v-model="formData!.remark"
                        prop="remark"
                        :input-attr="{ rows: 3 }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="onSubmit"
                        placeholder="请输入备注信息"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="popUp.$state.FromState = false">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="onSubmit" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import { usePopupFromStore } from './popupFrom'
import { baTableApi } from '/@/api/common'

const config = useConfig()
const formRef1 = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const popUp = usePopupFromStore()

const { t } = useI18n()
const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    parts_cost: [buildValidatorData({ name: 'number', title: t('parts.info.parts_cost') })],
    parts_price: [buildValidatorData({ name: 'number', title: t('parts.info.parts_price') })],
    update_time: [buildValidatorData({ name: 'date', title: t('parts.info.update_time') })],
    create_time: [buildValidatorData({ name: 'date', title: t('parts.info.create_time') })],
})

//创建表单数据
const formData = reactive({
    name: '',
    parts_cost: 0,
    parts_price: 0,
    purpose_info_id: '',
    supplier_info_id: '',
    remark: '',
})

const onSubmit = () => {
    const newApi = new baTableApi('/admin/parts.info/')
    //提交数据
    newApi.postData('add', formData).then((res) => {
        console.log(res)
        popUp.$state.FromState = false
        if (baTable.form.items) {
            baTable.form.items.partsInfo.name = formData.name
            baTable.form.items.purpose_info_id = formData.purpose_info_id
            baTable.form.items.supplier_info_id = formData.supplier_info_id
            baTable.form.items.parts_cost = formData.parts_cost
            newApi.index({ search: [{ field: 'name', val: formData.name, operator: '=' }] }).then((res) => {
                baTable.form.items.parts_info_id = res.data.list[0].id
            })
        }
    })
}
</script>

<style scoped lang="scss"></style>
